package com.example.hehecat.yuejian1.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SearchView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.hehecat.yuejian1.R;
import com.example.hehecat.yuejian1.mvp.contract.ListContract;
import com.example.hehecat.yuejian1.mvp.model.bean.ClubBean;
import com.example.hehecat.yuejian1.mvp.presenter.ClubListPresenter;
import com.example.hehecat.yuejian1.ui.adapter.ClubListAdapter;
import com.example.hehecat.yuejian1.ui.wiget.MyItemDecoration;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static org.greenrobot.eventbus.EventBus.TAG;


public class ClubFragment extends Fragment implements ListContract.View<ClubBean> {

    @BindView(R.id.club_list)
    RecyclerView clubList;
    Unbinder unbinder;
    @BindView(R.id.search_club)
    SearchView searchClub;
    private ClubListAdapter adapter;
    private ClubListPresenter presenter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_club_list, container, false);
        unbinder = ButterKnife.bind(this, view);

        //创建布局管理
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        clubList.setLayoutManager(layoutManager);
        clubList.addItemDecoration(new MyItemDecoration());

        //创建适配器
        adapter = new ClubListAdapter(R.layout.item_list, getContext());
        adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        adapter.setNotDoAnimationCount(2);

        //监听item点击
        adapter.setOnItemClickListener((madapter, view1, position) ->
                moveToclubInfo((adapter.getItem(position)).getClubid()));

        //给RecyclerView设置适配器
        presenter = new ClubListPresenter(this);
        presenter.updateAdapterData();
        clubList.setAdapter(adapter);

        //设置搜索
        searchClub.setQueryHint("搜索俱乐部");
        searchClub.setSubmitButtonEnabled(true);
        searchClub.setOnCloseListener(() -> {
            Toast.makeText(getContext(), "Close", Toast.LENGTH_SHORT).show();
            return false;
        });
        searchClub.setOnSearchClickListener(v ->
                Toast.makeText(getContext(), "Open", Toast.LENGTH_SHORT).show());

        searchClub.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                presenter.updateSearchData(s);
                adapter = new ClubListAdapter(R.layout.item_list, getContext());

                clubList.setAdapter(adapter);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                Log.e("CSDN_LQR", "TextChange --> " + s);
                return false;
            }
        });

        return view;
    }


    public static ClubFragment newInstance(String name) {
        ClubFragment fragment = new ClubFragment();
        Bundle args = new Bundle();
        args.putString("name", name);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void updateAdapterData(List<ClubBean> data) {
        adapter.addData(data);
    }

    @Override
    public void moveToclubInfo(int club_id) {
        ClubInfoFragment Fragment = ClubInfoFragment.newInstance(club_id);
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content, Fragment, null)
                .commit();
    }


}
